package com.yy.yyui.tab
{
	import com.siku.manor.view.common.OutOneBackGround;
	import com.siku.manor.view.common.UM;
	import com.siku.manor.view.component.GT;
	import com.siku.manor.view.component.ISelectable;
	import com.siku.manor.view.component.button.RadioButton;
	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class TabPanel extends Sprite
	{
		
		private var w:Number,h:Number;
		private var _horizontalGap:Number;
		
		public var bg:DisplayObject;
		private var invertMask:Shape;
//		private var backgroundPanel:Sprite;
		
		private var _contentPanel:Sprite;
		
		
		private var tabPanel:Sprite;
		
		private static const topMargin:Number = 7;
		private static const leftMargin:Number = 8;
		private static const rightMargin:Number = 8;
		private static const bottomMargin:Number = 7;
		private static const tabPanelMargin:Number = -1;
		
		private var tabPanelHeight:Number = 28;
		
		private var backgroundPanel:Sprite;
		
		
//		private var bgData:BitmapData;				
		public function TabPanel(w:Number,h:Number,horizontalGap:Number = 1,tabHeight:Number = 28,darker:Boolean=false)
		{
			//TODO: implement function
			super();
			this.w = w;
			this.h = h;
			this._horizontalGap = horizontalGap;
			this.tabPanelHeight = tabHeight;
			var dobj:DisplayObject;
			var sp:Sprite;
			
//			//backgroundpane
			sp = new Sprite();
			dobj = this.addChild(sp);
			backgroundPanel = sp;
			backgroundPanel.addEventListener(MouseEvent.CLICK,onTabClick);
			
			//bg
			bg = UM.scale9(new OutOneBackGround());
			dobj = this.addChild(bg);
			dobj = bg;
			dobj.x = 0;
			dobj.y = tabPanelHeight;
			dobj.width = this.width;
			dobj.height = this.height-tabPanelHeight;
			
			var sh:Shape = new Shape();
			GT.fulFill3(sh,bg.width,bg.height);
			dobj = this.addChild(sh);
			dobj.x = bg.x;
			dobj.y = bg.y;
			bg.mask = sh;
			this.invertMask = sh;
			
			//tabpanel
			tabPanel = new Sprite();
			this.addChild(tabPanel);
		}
		
		public function setSelectedTab(tab:RadioButton):void{
			tab.selected = true;
		}
		private var selectedTab:Boolean = false;
		private function onTabClick(e:MouseEvent):void{
			if(e.target is ISelectable){
//				var dobj:DisplayObject; 
//				if(this.selectedTab)
//				{
//					var rDobj:DisplayObject = this.tabPanel.getChildAt(0);
//					this.backgroundPanel.addChild(rDobj);
//				}else
//				{
//					this.backgroundPanel.addChild(rDobj);
//				}
//				dobj = this.tabPanel.addChild((e.target as DisplayObject));
//				dobj.x = e.target.x;
//				dobj.y = e.target.y;
//				this.selectedTab = true; 
				this.updateBackground(e);
			}
		}
		private var ss:int = 1;
		public function updateBackground(e:MouseEvent = null,clear:Boolean= false):void{
			var i:uint,l:uint;
			var dobj:DisplayObject;
			var g:Graphics;
			var rect:Rectangle;
			g = this.invertMask.graphics;
			g.clear();
			if(clear) return;
			dobj = e.target as DisplayObject;
			g.beginFill(0xff0000);
			g.drawRect(dobj.x+dobj.width,0,this.w,this.h);
			g.drawRect(0,0,dobj.x,this.h);
			g.drawRect(dobj.x,dobj.y+2,dobj.width,this.h);
			g.endFill();
		}
		
		public function addTab(tab:DisplayObject):DisplayObject{
			var i:uint,l:uint;
			var maxRight:Number = 0;
			var dobj:DisplayObject; 
			for(i = 0,l = this.backgroundPanel.numChildren;i<l;i++){
				dobj = this.backgroundPanel.getChildAt(i);
				if(dobj==tab){
					return tab;
				}
				if(maxRight<dobj.x+dobj.width){
					maxRight = dobj.x+dobj.width;
				}
			}
			tab.x = maxRight+horizontalGap;
			tab.y = 0;
			dobj = this.backgroundPanel.addChild(tab);
			return dobj;
		}
		
		public function addTextTab(label:String,color:Object = null,w:Number = 45,h:Number = 30):RadioButton{
			var rb:RadioButton = UM.createLabelTab(label,w,h,color,false,false);
			this.addTab(rb);
			return rb;
		}
		
		public function removeTab(tab:DisplayObject):DisplayObject{
			var dobj:DisplayObject;
			if(tab!=null&&this.tabPanel.contains(tab)){
				var rr:Rectangle = tab.getBounds(this.tabPanel);
				var diff:Number = rr.width+this.horizontalGap;
				this.tabPanel.removeChild(tab);
				var i:uint,l:uint;
				for(i = 0,l = this.tabPanel.numChildren;i<l;i++){
					dobj = this.tabPanel.getChildAt(i);
					if(dobj.x>rr.x){
						dobj.x -= diff;
					}
				}			
			}
			return dobj;
		}
		
		public function getTabByIndex(index:uint):DisplayObject{
			var dobj:DisplayObject;
			if(index<this.tabPanel.numChildren){
				dobj = this.tabPanel.getChildAt(index);
			}
			return dobj;
		}
		
		public function get contentPanel():Sprite{
			return _contentPanel;
		}
		
		public function get horizontalGap():Number{
			return this._horizontalGap;
		}
		

		override public function get width():Number{
			return w;
		}
		
		override public function get height():Number{
			return h;
		}
		
		override public function set width(newWidth : Number) : void {
			var diff:Number = newWidth - w;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;			
			w = newWidth;
		}
		
		override public function set height(newHeight : Number) : void {
			var diff:Number = newHeight - h;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;			
			h = newHeight;			
		}
		

	}
	
}